﻿using System.Diagnostics;
using System.IO;
using System.IO.Compression;

namespace DNFImagePacks2Manager.Helpers
{
    public class ZLibHelper
    {
        public static byte[] DecompressZlib(byte[] compressedData)
        {
            if (compressedData == null || compressedData.Length == 0)
            {
                return [];
            }

            using var inputStream = new MemoryStream(compressedData);
            using var zlibStream = new ZLibStream(inputStream, CompressionMode.Decompress);
            using var outputStream = new MemoryStream();
            zlibStream.CopyTo(outputStream);

            return outputStream.ToArray();
        }

        public static byte[] CompressZlib(byte[] rawData)
        {
            if (rawData == null || rawData.Length == 0)
            {
                return [];
            }

            using var outputStream = new MemoryStream();
            using (var zlibStream = new ZLibStream(outputStream, CompressionMode.Compress))
            using (var inputStream = new MemoryStream(rawData))
            {
                inputStream.CopyTo(zlibStream);
            }
            return outputStream.ToArray();
        }
    }
}
